package gui.panels;

import gui.abstracts.AbstractPanel;
import gui.abstracts.AbstractTable;
import gui.containerDate.Item;
import gui.tables.TableGroup;
import gui.tables.models.MyTableModel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import bean.GroupBean;
import bean.UniversityBean;

import commandPatern.Delete;
import commandPatern.Insert;
import commandPatern.StoreCommands;
import commandPatern.Update;

import dao.GroupDAO;
import dao.UniversityDAO;

import patern.*;

public class PanelGroup extends AbstractPanel {
	private JScrollPane scrollTableGroup;

	private Insert insert;
	private static PanelGroup panelGroup;

	private PanelGroup() {

		initGui();
		Sybject.getInstance().Attach(PanelStudent.getInstance());
	}

	public static PanelGroup getInstance() {
		if (panelGroup == null) {
			panelGroup = new PanelGroup();
		}
		return panelGroup;

	}

	public void initGui() {

		BoxLayout layoutGroup = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(layoutGroup);
		jAbstractTable = new TableGroup();
		scrollTableGroup = new JScrollPane(jAbstractTable);
		this.add(scrollTableGroup);
	}

	public AbstractTable getTable() {
		return this.jAbstractTable;
	}

	public void initTable() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		GroupDAO GrDAO = new GroupDAO();
		ArrayList<GroupBean> group = GrDAO.InitGroup();

		for (GroupBean groupBean : group) {

			System.out.println(groupBean.getRow());
			((TableGroup) jAbstractTable).getDefaultTableMOdel().addRow(
					groupBean.getRow());

			Sybject.getInstance().addNotifyObs(
					new Item(groupBean.getId(), groupBean.getDenumire()));
		}

	}

	public UniversityBean getUniversityBean(int i) {
		UniversityBean univDAO = null;

		if (i == -1) {
			i = jAbstractTable.getRowCount() - 1;
			univDAO = new GroupBean(0,
					(String) jAbstractTable.getValueAt(i, 1),
					(String) jAbstractTable.getValueAt(i, 2));
		} else {
			System.out.println("in functia getUniversituBean"
					+ (Integer) jAbstractTable.getValueAt(i, 0));
			univDAO = new GroupBean((Integer) jAbstractTable.getValueAt(i, 0),
					(String) jAbstractTable.getValueAt(i, 1),
					(String) jAbstractTable.getValueAt(i, 2));
		}

		return univDAO;
	}

	public void removeRowFromTable() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

		UniversityBean univBean = null;
		univBean = getUniversityBean(jAbstractTable.getSelectedRow());

		Sybject.getInstance().removeNotifyOps(
				new Item(((GroupBean) univBean).getId(), ((GroupBean) univBean)
						.getDenumire()));

		super.removeRowFromTable();

	}

	@Override
	public void cancelEdit() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

		UniversityDAO grDAO = new GroupDAO();
		super.cancelEditTotal(grDAO);
	}

	public void insertObjectInDb() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		UniversityBean un = getUniversityBean(-1);
		UniversityDAO uDAO = un.getDAO();
		int iD = uDAO.Insert(un);

		un.setId(iD);

		StoreCommands.getInstance().add(new Insert(un, jAbstractTable));

		jAbstractTable.setIdAfterInsert(iD);

		Sybject.getInstance().addNotifyObs(
				new Item(iD, jAbstractTable.getValueAt(
						jAbstractTable.getRowCount() - 1, 1).toString()));

		((MyTableModel) jAbstractTable.getDefaultTableMOdel())
				.setInsertNewRow(false);
	}

}
